package repeat.personal.util;


import org.apache.commons.lang.StringUtils;
import repeat.personal.page.PageRequest;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Zhanggq on 2019/5/22.
 */
public class JpaSortUtils {

	public static final String ORDER_DESC_KEY="DESC";

	public static final String ORDER_ASC_KEY="ASC";


	public static final String SPLIT_KEY="#";


	public static List<Order> buidOrders(PageRequest request, Root root, CriteriaBuilder cb){

		String desc = request.getStringParam(ORDER_DESC_KEY);
		String asc = request.getStringParam( ORDER_ASC_KEY);
		if (!org.apache.commons.lang.StringUtils.isBlank(desc)){
			return Arrays.stream(desc.split(SPLIT_KEY)).map(s -> {
				return cb.desc(root.get(s));
			}).collect(Collectors.toList());
		}
		if (!StringUtils.isBlank(asc)){
			return Arrays.stream(asc.split(SPLIT_KEY)).map(s -> {
				return cb.desc(root.get(s));
			}).collect(Collectors.toList());
		}
		return new ArrayList<>(0);
	}



}
